//
//  ServiceHolder+Table.swift
//  CooperSwift
//
//  Created by 计海峰 on 2022/2/23.
//

import Foundation

// MARK: - 转接 - table
public extension ServiceHolder {
    
    /// 通过key搜索tablle的indexpath
    func byKeySearchIndexPath(_ parent: TableViewModelProtocol, key: String) -> IndexPath? {
        for (sectionIdx, section) in parent.sections.enumerated() {
            for (index, model) in section.items.enumerated() {
                if let info = model as? BindKeyProtocol {
                    if info.bindKey() == key {
                        return IndexPath(row: index, section: sectionIdx)
                    }
                }
            }
        }
        return nil
    }
    
    /// reload cell
    @objc func serviceReloadTableCell(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.serviceReloadTableCell(key, parent: parent)
            }
            return
        }
        guard let tv = parentTarget.currentTV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        DispatchQueue.main.async {
            tv.reloadRows(at: [indexPath], with: .none)
        }
    }
    
    /// reload section
    @objc func serviceReloadTableSection(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.serviceReloadTableSection(key, parent: parent)
            }
            return
        }
        guard let tv = parentTarget.currentTV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        
        DispatchQueue.main.async {
            tv.reloadSections([indexPath.section], animationStyle: .none)
        }
    }
    
    /// reload table
    @objc func serviceReloadTable(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        parentTarget.reloadTable()
//        guard let tv = parentTarget.currentTV() else { return }
//        parentTarget.isRefreshing = true
//        DispatchQueue.main.async {
//            tv.reloadData()
//        }
//        parentTarget.isRefreshing = false
    }
    
    /// remove cell
    @objc func serviceRemoveTableCell(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.serviceRemoveTableCell(key, parent: parent)
            }
            return
        }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        guard var section = parentTarget.sections[safe: indexPath.section] else { return }
        section.items.remove(at: indexPath.row)
        parentTarget.sections[indexPath.section] = section
    }
    
    /// remove section
    @objc func serviceRemoveTableSection(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.serviceRemoveTableSection(key, parent: parent)
            }
            return
        }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        parentTarget.sections.remove(at: indexPath.section)
        guard let tv = parentTarget.currentTV() else { return }
        DispatchQueue.main.async {
            tv.reloadData()
        }
    }
    
    /// toTableCellTop
    @objc func toTableCellTop(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.toTableCellTop(key, parent: parent)
            }
            return
        }
        guard let tv = parentTarget.currentTV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        DispatchQueue.main.async {
            tv.scrollToRow(at: indexPath, at: .top, animated: false)
        }
    }
    
    /// toTableCellBottom
    @objc func toTableCellBottom(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.toTableCellBottom(key, parent: parent)
            }
            return
        }
        guard let tv = parentTarget.currentTV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        DispatchQueue.main.async {
            tv.scrollToRow(at: indexPath, at: .bottom, animated: false)
        }
    }
    
    /// update table
    @objc func serviceUpdatesTable(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.serviceUpdatesTable(key, parent: parent)
            }
            return
        }
        guard let tv = parentTarget.currentTV() else { return }
        DispatchQueue.main.async {
            tv.beginUpdates()
            tv.endUpdates()
        }
    }
    
    /// update row height
    @objc func serviceUpdatesRowHeight(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.serviceUpdatesTable(key, parent: parent)
            }
            return
        }
        guard let tv = parentTarget.currentTV() else { return }
        guard let indexPath = byKeySearchIndexPath(parentTarget, key: key) else { return }
        
        DispatchQueue.main.async {
            tv.reloadRows(at: [indexPath], with: .none)
        }
        
    }
    
    /// 隐藏键盘
    @objc func serviceHiddenKeyboard(_ key: String, parent: NSObject) {
        guard let parentTarget = parent as? TableViewModelProtocol else { return }
        if parentTarget.isRefreshing {
            PublicMethod.delay(0.3) { [weak self] in
                self?.serviceHiddenKeyboard(key, parent: parent)
            }
            return
        }
        guard let tv = parentTarget.currentTV() else { return }
        tv.endEditing(true)
        guard let v = parentTarget.currentV() else { return }
        v.endEditing(true)
    }
    
//    /// 刷新数据
//    @objc func serviceReloadData(_ key: String, parent: NSObject) {
//        
//    }
//    
//    /// 刷新 forRows
//    @objc func serviceReForRows(_ key: String, parent: NSObject) {
//        
//    }
    
}
